package com.school.edupoint.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.edupoint.config.EduContext;
import com.school.edupoint.mapper.StudentActivityHistoryMapper;
import com.school.edupoint.model.Activity;
import com.school.edupoint.model.Student;
import com.school.edupoint.model.StudentActivityHistory;
import com.school.edupoint.model.StudentGift;
import com.school.edupoint.vo.HomeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentActivityHistoryService {
    @Autowired
    private EduContext eduContext;
    @Autowired
    private StudentActivityHistoryMapper studentActivityHistoryMapper;

    /**
     * 增加
     *
     * @param history
     * @return
     */
    public StudentActivityHistory save(StudentActivityHistory history) {
        studentActivityHistoryMapper.insert(history);
        return history;
    }

    public void updateByASId(String activityId, Integer studentId, StudentActivityHistory history) {
        studentActivityHistoryMapper.updateByASId(activityId, studentId, history);
    }

    public StudentActivityHistory selectIsP(String activityId, Integer studentId) {
        return studentActivityHistoryMapper.selectIsP(activityId, studentId);
    }

    /**
     * 分页、模糊查询
     *
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<StudentActivityHistory> selectPage(String title, int pageNum, int pageSize) {
        // 创建分页对象
        Page<StudentActivityHistory> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<StudentActivityHistory> wrapper = new LambdaQueryWrapper<>();
        // 如果 title 有值，则加上模糊查询条件
        if (title != null && !title.isEmpty()) {
            wrapper.like(StudentActivityHistory::getActivityName, title);
        }
        // 统一按创建时间降序排序
        wrapper.orderByDesc(StudentActivityHistory::getCreateTime);

        return studentActivityHistoryMapper.selectPage(page, wrapper);
    }

    public HomeVO selectInfo() {
        HomeVO homeVO = studentActivityHistoryMapper.selectInfo();
        System.out.println("总报名人数" + homeVO.getTotalRecords());
        System.out.println("总签到" + homeVO.getTotalCheckIn());
        int totalRecords = homeVO.getTotalRecords();
        int totalCheckIn = homeVO.getTotalCheckIn();

        if (totalRecords > 0) {
            float checkInRate = (float) totalCheckIn / totalRecords;
            String formattedRate = String.format("%.2f%%", checkInRate * 100);

            homeVO.setCheckInRate(checkInRate);         // 设置浮点型签到率
            homeVO.setCheckInRateStr(formattedRate);    // 设置格式化后的百分比字符串
        } else {
            homeVO.setCheckInRate(0f);
            homeVO.setCheckInRateStr("0.00%");
        }

        // 获取参加人数最多的前5个活动
        homeVO.setTopActivities(selectTopActivities(5));

        return homeVO;
    }

    public Integer selectTotalPoints() {
        return studentActivityHistoryMapper.selectTotalPoints();
    }

    /**
     * 打卡记录
     *
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<StudentActivityHistory> mylist(Integer studentId, String title, int pageNum, int pageSize) {
        // 创建分页对象
        Page<StudentActivityHistory> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<StudentActivityHistory> wrapper = new LambdaQueryWrapper<>();
        // 如果 title 有值，则加上模糊查询条件
        if (title != null && !title.isEmpty()) {
            wrapper.like(StudentActivityHistory::getActivityName, title);
        }
        // 统一按创建时间降序排序
        wrapper.orderByDesc(StudentActivityHistory::getId);
        wrapper.eq(StudentActivityHistory::getStudentId, studentId);

        return studentActivityHistoryMapper.selectPage(page, wrapper);
    }

    public Integer selectStatusTwoCount() {
        return studentActivityHistoryMapper.selectStatusTwoCount();
    }

    public Integer selectStatusTwoPointCount() {
        return studentActivityHistoryMapper.selectStatusTwoPointCount();
    }

    // 已不再使用
    // public String selectMaxActivityTitle() {
    //     return studentActivityHistoryMapper.selectMaxActivityTitle();
    // }
    //
    // public String selectMaxActivityImg() {
    //     return studentActivityHistoryMapper.selectMaxActivityImg();
    // }

    public List<StudentActivityHistory> findByStudentIdAndActivityIds(Integer studentId) {
        return studentActivityHistoryMapper.selectByStudentIdAndActivityIds(studentId);
    }

    /**
     * 查询参加人数最多的前N个活动
     * @param limit 返回记录数限制
     * @return 包含活动标题和图片的Map列表
     */
    public Map<String, String> selectTopActivities(int limit) {
        List<Map<String, Object>> topActivities = studentActivityHistoryMapper.selectTopActivities(limit);
        Map<String, String> activityMap = new HashMap<>();
        for (Map<String, Object> activity : topActivities) {
            String title = (String) activity.get("title");
            String img = (String) activity.get("poster");
            // 过滤掉null键，或者将null替换为默认值
            if (title != null) {
                activityMap.put(title, img != null ? img : "");
            }
        }
        return activityMap;
    }


    public List<Activity> PA() {
        return studentActivityHistoryMapper.selectPA();
    }
}